package org.opsli.modulars.business.analysisExcel.web;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.opsli.api.wrapper.business.patrolStation.PatrolStationModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.modulars.business.analysisExcel.entity.alsExcel;
import org.opsli.modulars.business.analysisExcel.service.IExcelService;
import org.opsli.modulars.business.patrolStation.service.IPatrolStationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Api(tags="app所需接口二类")
@Slf4j
@ApiRestController("/appExcel/two")
public class ReadExcelUtils {

    @Autowired
    private IExcelService iExcelService;

    public String nameList;

//    @Autowired
//    IPatrolStationService iPatrolStationService;


/*    public  List<alsExcel> read2007Xlsx(InputStream in) throws IOException, InvalidFormatException, ParseException {

        List<alsExcel> list = new ArrayList<>();
        XSSFWorkbook xWorkbook = new XSSFWorkbook(in);
        // Read the Sheet
        XSSFSheet xssfSheet = xWorkbook.getSheetAt(0);

        // Read the Row
        for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
            XSSFRow xssfRow = xssfSheet.getRow(rowNum);
            if (xssfRow != null) {
                alsExcel order =new alsExcel();
                // 单元格从 0 开始
                order.setDevid("123");
                String name=xssfRow.getCell(1).getStringCellValue();
                if(!name.equals("")){
                    order.setHousingName(name);
                    nameList=name;
                }else {
                    order.setHousingName(nameList);
                }
                order.setLocation(xssfRow.getCell(2).getStringCellValue());
                order.setConditions(xssfRow.getCell(3).getStringCellValue());
//                iExcelService.insertUser(order);
                list.add(order);

            }

        }
//        int l= iExcelService.inster(list);
        xWorkbook.close();
        return list;
    }*/
    public  List<PatrolStationModel> read2007Xlsx(InputStream in) throws IOException, InvalidFormatException, ParseException {

        List<PatrolStationModel> list = new ArrayList<>();
        XSSFWorkbook xWorkbook = new XSSFWorkbook(in);
        // Read the Sheet
        XSSFSheet xssfSheet = xWorkbook.getSheetAt(0);

        // Read the Row
        for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
            XSSFRow xssfRow = xssfSheet.getRow(rowNum);
            if (xssfRow != null) {
                PatrolStationModel model =new PatrolStationModel();
                // 单元格从 0 开始
                model.setDevid("123"); //公司id
                model.setDevname("演示公司");//公司名称
                //类型4 调压箱
                model.setType("4");
                //调压箱小区名称
                String name=xssfRow.getCell(1).getStringCellValue();
                if(!name.equals("")){
                    model.setVillageName(name);
                    nameList=name;
                }else {
                    model.setVillageName(nameList);
                }
                model.setLocation(xssfRow.getCell(2).getStringCellValue());

//                iPatrolStationService.insert(model);
                list.add(model);

            }

        }
//        int l= iExcelService.inster(list);
        xWorkbook.close();
        return list;
    }

    public static void main(String[] args) throws InvalidFormatException, IOException, ParseException {
        String fullFileName = "C:\\Users\\HY\\Downloads\\调压设备.xlsx";
        InputStream is = new FileInputStream(fullFileName);
        ReadExcelUtils readExcelUtils=new ReadExcelUtils();
        List<PatrolStationModel> list =readExcelUtils.read2007Xlsx(is);
        System.out.println("size==>" + list.size());
        for (PatrolStationModel list1:list){
            System.out.println(list1.getDevid()+"---"+list1.getVillageName()+"---"+list1.getLocation()+"---"+list1.getLocation());
        }
    }

}

